/*
 * Copyright (c) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.services.samples.translate.cmdline;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;
import com.google.api.translate.Language;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Main class for the Translate API command line sample.
 */
public class TranslateSample
{

    /**
     * Be sure to specify the name of your application. If the application name
     * is {@code null} or blank, the application will log a warning. Suggested
     * format is "MyCompany-ProductName/1.0".
     */
    private static final String APPLICATION_NAME = "Translator";

    /**
     * API Key for the registered developer project for your application.
     */
    private static final String API_KEY = "";

    /**
     * Global instance of the JSON factory.
     */
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    /**
     * Global instance of the HTTP transport.
     */
    private static HttpTransport httpTransport;

    @SuppressWarnings("unused")
    private static Translate client;

    public static void googleTranslateInit(String apiKey)
    {
        String answer = null;
        try
        {
            // initialize the transport
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();

            // set up global Translate instance
            client = new Translate.Builder(httpTransport, JSON_FACTORY, null)
                    .setGoogleClientRequestInitializer(new TranslateRequestInitializer(apiKey))
                    .setApplicationName(APPLICATION_NAME).build();

        } catch (IOException e)
        {
            System.err.println(e.getMessage());
        } catch (Throwable t)
        {
            t.printStackTrace();
        }

         
    }

    public static String googleTranslate(String forTranslation, String targetLanguage,String sourceLanguage)
    {
        String answer = null;
        try
        {
            ArrayList al = new ArrayList();
            al.add(forTranslation);
            Translate.Translations.List vv = client.translations().list(al, targetLanguage);
            System.out.println(vv.toString());
            TranslationsListResponse response = vv.execute();
//                        client.translate("Hello", Language.ENGLISH, Language.FRENCH);

            System.out.println("Hello =" + response.getTranslations().get(0).toString());
            TranslationsResource x = response.getTranslations().get(0);
            answer = x.getTranslatedText();
        } catch (IOException e)
        {
            System.err.println(e.getMessage());
        } catch (Throwable t)
        {
            t.printStackTrace();
        }

        return answer;
    }

//    public static void main(String[] args)
//    {
//        
//        googleTranslateInit(API_KEY);
//        String st = googleTranslate("Hello", "fr", "en");
//        System.out.println("Hello =" + st.toString());
//        
//
//        System.exit(1);
//    }
}
